import com.alibaba.fastjson.JSON;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("application/json;charset=utf-8");
        System.out.println("ceshi...");
        String name = req.getParameter("name");
        System.out.println(name);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        System.out.println(dataSource);
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from user where name like '%"+name+"%' ";
        try {
            List<User> query = queryRunner.query(sql, new BeanListHandler<User>(User.class));
            System.out.println(query.size());
            String use = JSON.toJSONString(query);
            System.out.println(use);
            res.getWriter().print(use);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
